<?php
/*
 * @file
 * Install file that creates all the tables in the DONATE database.
 *
 * Copyright 2011 by Matt Edmonds, Matt Vartanian.
 * This program is part of DONATE.
 * DONATE is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 *
 * @version last updated 7/28/2011
 * @version created 3/11/2011
 * @author Matt Edmonds
 * @author Matt Vartanian
 */
function createDatabaseAndTables($host, $database, $username, $password)
{
	echo("Installing Donate database...<br />");

	include_once('dbDonation.php');
	include_once('dbDonor.php');
	include_once('dbEvent.php');
	include_once('dbEventType.php');
	include_once('dbOrganization.php');
	include_once('dbIndividual.php');

	echo("Including files completed.<br />");

	$db_server = mysql_connect($host, $username, $password);
	if (!$db_server) {
		die("Cannot connect to $host server." . mysql_error( ));
	}
	echo("Connected to server: $host with username: $username and
          password: $password.<br /><br />");

	$result = mysql_query("DROP DATABASE IF EXISTS Donate");
	if (!$result) {
		die("Cannot drop database Donate." . mysql_error( ));
	}
	echo("Existing Donate database dropped.<br />");

	$result = mysql_query("CREATE DATABASE Donate");
	if (!$result) {
		die("Cannot create database Donate." . mysql_error( ));
	}
	echo("Database Donate created. <br />");

	$result = mysql_query("SET storage_engine = InnoDB");
	if (!$result) {
		die("Cannot set storage engine to InnoDB." . mysql_error( ));
	}
	mysql_close( );

	echo("<br />Creating tables...<br />");

	$result = dbEventType::createTable();
	if (!$result) {
		die("Could not create table 'EventType': " . mysql_error());
	}
	echo ("Added table 'EventType' successfully...<br />");

	/**
	 * Initializes the Event table
	 */
	$result = dbEvent::createTable();
	if (!$result) {
		die("Could not create table 'Event': " . mysql_error());
	}
	echo ("Added table 'Event' successfully...<br />");

	/**
	 * Initializes the Donor table
	 */
	$result = dbDonor::createTable();
	if (!$result) {
		die("Could not create table 'Donor': " . mysql_error());
	}
	echo ("Added table 'Donor' successfully...<br />");

	/**
	 * Initializes the Donation table
	 */
	$result = dbDonation::createTable();
	if (!$result) {
		die("Could not create table 'Donation': " . mysql_error());
	}
	echo ("Added table 'Donation' successfully...<br />");

	/**
	 * Initializes the individual table
	 */
	$result = dbIndividual::createTable();
	if (!$result) {
		die("Could not create table 'Individual': " . mysql_error());
	}
	echo ("Added table 'Individual' successfully...<br />");

	/**
	 * Initializes the organization table
	 */
	$result = dbOrganization::createTable();
	if (!$result) {
		die("Could not create table 'Organization': " . mysql_error());
	}
	echo ("Added table 'Organization' successfully...<br />");

	echo("<br />Installation of mysql tables complete.");
}

function populateDatabase( )
{
	include_once('dbDonation.php');
	include_once('dbDonor.php');
	include_once('dbEvent.php');
	include_once('dbEventType.php');
	include_once('dbOrganization.php');
	include_once('dbIndividual.php');
	$id = 0;
	dbEventType::addEventTypeByFields('Delicious dinner', 'A fantastic event', $id);
	dbEventType::verifyInsertedEventType($id);

	dbEventType::addEventTypeByFields('Fundraiser Dinner', 'A spectacular event', $id);
	dbEventType::verifyInsertedEventType($id);

	dbEventType::addEventTypeByFields('Donation event', 'A great event', $id);
	dbEventType::verifyInsertedEventType($id);

	dbEventType::addEventTypeByFields('Fundraiser', 'A spectacular event', $id);
	dbEventType::verifyInsertedEventType($id);

	dbEventType::addEventTypeByFields('Dinner', 'Dinner at the radisson', $id);
	dbEventType::verifyInsertedEventType($id);
	echo("<br />");
	/**
	 * Adds data to the event table
	 */
	dbEvent::addEventByFields(1,"2011-2-27","26 Elm St.", "Baton Rouge","LA","02722-5511", $id);
	dbEvent::verifyInsertedEvent($id);

	dbEvent::addEventByFields(2,"2012-03-01","100 Beacon St.","Boston","MA","02134-0211", $id);
	dbEvent::verifyInsertedEvent($id);

	dbEvent::addEventByFields(3,'2011-3-1', '354 Johnson St', 'Manchester', 'NH', '03445', $id);
	dbEvent::verifyInsertedEvent($id);

	dbEvent::addEventByFields(4,'2011-3-1', '200 Smith St', 'Manchester', 'NH', '03445', $id);
	dbEvent::verifyInsertedEvent($id);

	dbEvent::addEventByFields(5,'2011-3-1', '760 Richard St', 'Manchester', 'NH', '03445', $id);
	dbEvent::verifyInsertedEvent($id);
	echo("<br />");
	/**
	 * Adds data to the donor table
	 */
	$columns = array('street'=>"45 Willow", 'city'=>"Manchester", 'stateName'=>"NH", 'zip'=>"03033", 'phone'=>"888-777-666", 'fax'=>"888-777-888", 'email'=>"willow@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>"22 Easy Street", 'city'=>"Manchester", 'stateName'=>"NH", 'zip'=>"02029", 'phone'=>"111-111-1111", 'fax'=>"222-222-2222", 'email'=>"testing@test.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'55 Smith street', 'city'=>'Lebanon', 'stateName'=>"NH", 'zip'=>"03044", 'phone'=>"603-660-5656", 'fax'=>"603-660-8888", 'email'=>"info@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'55 Smith street', 'city'=>'Lebanon', 'stateName'=>"NH", 'zip'=>"03044", 'phone'=>"603-660-5656", 'fax'=>"603-660-8888", 'email'=>"info@gmail.com");	
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'44 normand road', 'city'=>'Manchester', 'stateName'=>"NH", 'zip'=>"03044", 'phone'=>"603-660-2828", 'fax'=>"603-660-8998", 'email'=>"info@live.com");	
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'89 Lumbar Street', 'city'=>'Manchester', 'stateName'=>"NH", 'zip'=>"03033", 'phone'=>"767-743-3325", 'fax'=>"122-345-322", 'email'=>"cp675@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'97 Smith Street', 'city'=>'Manchester', 'stateName'=>"NH", 'zip'=>"03533", 'phone'=>"777-567-4532", 'fax'=>"113-990-247", 'email'=>"gbd5673@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'13 Acre Street', 'city'=>'Manchester', 'stateName'=>"NH", 'zip'=>"22233", 'phone'=>"767-788-3757", 'fax'=>"223-444-709", 'email'=>"ugh665@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'90 Paper Road', 'city'=>'Manchester', 'stateName'=>"NH", 'zip'=>"88907", 'phone'=>"767-743-3352", 'fax'=>"334-565-8989", 'email'=>"ffds765@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);

	$columns = array('street'=>'15 Store Street', 'city'=>'Manchester', 'stateName'=>"NH", 'zip'=>"88996", 'phone'=>"454-444-6675", 'fax'=>"909-608-1123", 'email'=>"vvf654@gmail.com");
	dbDonor::addDonorByFieldsInArray($columns, $id );
	dbDonor::verifyInsertedDonor($id);
	echo("<br />");
	/**
	 * Adds data to the donation table
	 */
	dbDonation::addDonationByFields("2011-5-6", 1, 1, $id);
	dbDonation::verifyInsertedDonation($id);

	dbDonation::addDonationByFields("2011-9-4", 2, 2, $id);
	dbDonation::verifyInsertedDonation($id);

	dbDonation::addDonationByFields('2011-4-11',3,3, $id);
	dbDonation::verifyInsertedDonation($id);

	dbDonation::addDonationByFields('2011-10-12',4,4, $id);
	dbDonation::verifyInsertedDonation($id);

	dbDonation::addDonationByFields('2011-8-1',5,5, $id);
	dbDonation::verifyInsertedDonation($id);
	echo("<br />");
	/**
	 * Adds data to the individual table
	 */
	dbIndividual::addIndividualByFields(1,'John','Smith', $id);
	dbIndividual::verifyInsertedIndividual(1);

	dbIndividual::addIndividualByFields(2,'Jayne','Cobb', $id);
	dbIndividual::verifyInsertedIndividual(2);

	dbIndividual::addIndividualByFields(3,'Malcolm','Reynolds', $id);
	dbIndividual::verifyInsertedIndividual(3);

	dbIndividual::addIndividualByFields(4,'Brad','Pitt', $id);
	dbIndividual::verifyInsertedIndividual(4);

	dbIndividual::addIndividualByFields(5,'Tom','Jones', $id);
	dbIndividual::verifyInsertedIndividual(5);
	echo("<br />");
	/**
	 * Adds data to the orginization table
	 */
	dbOrganization::addOrganizationByFields(6,'TomTom', 'GPS facility', $id);
	dbOrganization::verifyInsertedOrganization(6);

	dbOrganization::addOrganizationByFields(7,'IBM', 'International Buisness Machines', $id);
	dbOrganization::verifyInsertedOrganization(7);

	dbOrganization::addOrganizationByFields(8,'TeleAtlas', 'GPS facility', $id);
	dbOrganization::verifyInsertedOrganization(8);

	dbOrganization::addOrganizationByFields(9,'Comcast', 'Networking facility', $id);
	dbOrganization::verifyInsertedOrganization(9);

	dbOrganization::addOrganizationByFields(10,'Hertz', 'Car Rental', $id);
	dbOrganization::verifyInsertedOrganization(10);
	echo("<br />");
}//end of populateDatabase( ) function
?>

